using ExtendedMvc.Html;

namespace ExtendedMvc.Elements {
	public class CheckBox<TModel> : CheckBoxBase<CheckBox<TModel>, TModel> where TModel : class {
		public CheckBox(IModelViewHelper<TModel> helper, string memberName, object value, bool isChecked, string id = null) : base(helper, memberName, value, isChecked, id) { }
	}

	public class CheckBoxOption<TModel> : CheckBoxBase<CheckBoxOption<TModel>, TModel> where TModel : class {
		public CheckBoxOption(IModelViewHelper<TModel> helper, string memberName, object value, bool isChecked) : base(helper, memberName, value, isChecked, null) { }

		//protected override void SetId() {
		//    if (id == null) Id(string.Format("{0}_{1}", id, value));
		//    base.SetId();
		//}
	}

	public abstract class CheckBoxBase<T, TModel> : Input<T, TModel>
		where T : CheckBoxBase<T, TModel>
		where TModel : class {
		private bool _isChecked;

		protected CheckBoxBase(IModelViewHelper<TModel> helper, string memberName, object value, bool isChecked, string id = null)
			: base(helper, HtmlInputType.Checkbox, value, memberName, id) {
			IsChecked(isChecked);
		}

		public virtual T IsChecked(bool value = true) {
			_isChecked = value;
			return (T)this;
		}

		protected override LabelPosition defaultLabelPosition { get { return LabelPosition.After; } }

		protected override void Prepare() {
			if (_isChecked) SetAttribute(HtmlAttribute.Checked, HtmlAttribute.Checked);
			base.Prepare();
		}
	}
}
